var http = require('../../utils/http.js');
var util = require('../../utils/util.js');
var unionid = wx.getStorageSync('thisCode');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        btnLft: 10,
        yzfalge: false,
        birthDay: '',
        endDay: '',
        inputArr: ['', '', ''],
        imgUrl: http.imgUrl,
        minTime: '获取验证码',
        checkFlag: true,
        clickFlag: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var unionid = wx.getStorageSync('thisCode');
        if (!unionid) {
            wx.reLaunch({
                url: '/pages/userPower/userPower'
            })
            return
        }
        var endDay = util.formatTime(new Date);
        this.setData({
            endDay: endDay
        })
    },
    birthDayBtn(e) {

    },
    // 获取得到的值
    inputBtn(e) {
        let inputArr = http.inputArr(this.data.inputArr, e);
        this.setData({ inputArr: inputArr })
    },
    // 点击获取验证码
    obtainCode() {
        var unionid = wx.getStorageSync('thisCode');
        if (!this.data.yzfalge) {
            http.showModal('请先完成滑动验证', false, () => {})
            return
        }
        var reg = /^1[3|4|5|7|6|8|9][0-9]\d{8}$/
        if (!reg.test(this.data.inputArr[0])) {
            http.showModal('请输入正确的手机号码', false, () => {})
            return
        }
        http._post('Binding/send_code', { unionid: unionid, phone: this.data.inputArr[0] }, res => {
            let data = res.data;
            if (data.status == 1) {
                let minTime = 60;
                this.setData({ minTime: minTime })
                let timeInterval = setInterval(() => {
                    minTime--;
                    this.setData({ minTime: minTime })
                    if (minTime <= 0) {
                        this.setData({ minTime: '获取验证码' });
                        clearInterval(timeInterval)
                    }
                }, 1000)
            }
        }, res => {})

    },
    // 前往公众号查看
    gzhSee() {
        http.showModal('暂不支持，请前往公众号查看', false, () => {})
    },
    // 点击提交数据
    upDataBtn() {
        var unionid = wx.getStorageSync('thisCode');
        if (!this.data.yzfalge) {
            http.showModal('请先完成滑动验证', false, () => {})
            return
        }
        let inputArr = this.data.inputArr;
        this.setData({ clickFlag: false })
        http._post('Binding/binding', {
            unionid: unionid,
            name: inputArr[2],
            code: inputArr[1],
            phone: inputArr[0],
        }, res => {
            let data = res.data;
            this.setData({ clickFlag: true })
            if (data.status == 1) {
                http.showModal(data.info, false, () => {
                    wx.redirectTo({
                        url: '/pages/index/index'
                    })
                })
            }
        }, res => {})
    },
    checkBtn() {
        let checkFlag = !this.data.checkFlag;
        this.setData({ checkFlag: checkFlag })
    },
    onReady() {

    },

    onShow() {
        wx.hideShareMenu();
    },

    onHide() {

    },

    onUnload() {

    },

    onPullDownRefresh() {

    },
    onReachBottom() {

    },
    // 开始触摸事件
    yzDown(e) {
        let x = Number(e.touches[0].pageX) - 45;
        this.setData({ btnLft: x })
    },
    // 开始移动
    yzMove(e) {
        let x = Number(e.touches[0].pageX) - 45;
        if (x <= 10) {
            x = 10
        } else if (x >= 310) {
            x = 310
            this.setData({ yzfalge: true })
        }
        this.setData({ btnLft: x })
    },
    // 抬起手势
    zyEnd(e) {
        let x = Number(e.changedTouches[0].pageX) - 45;
        if (x < 310) {
            x = 10;
            this.setData({ btnLft: x })
        }
    },

})